import 'package:flutter/material.dart';
import 'package:intl/intl.dart';
import '../models/weather_model.dart';

class WeatherCard extends StatelessWidget {
  final CaiyunRealtime weather;
  final String city;
  final VoidCallback onTap;

  const WeatherCard({
    super.key,
    required this.weather,
    required this.city,
    required this.onTap,
  });

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      onTap: onTap,
      child: Container(
        padding: const EdgeInsets.all(20),
        decoration: BoxDecoration(
          color: Colors.white.withOpacity(0.9),
          borderRadius: BorderRadius.circular(20),
          boxShadow: [
            BoxShadow(
              color: const Color.fromARGB(255, 232, 231, 231).withOpacity(0.1),
              blurRadius: 10,
              offset: const Offset(0, 2),
            ),
          ],
          image: DecorationImage(
            image: AssetImage(_getWeatherBackground(weather.skycon)),
            fit: BoxFit.cover,
            colorFilter: ColorFilter.mode(
              Colors.black.withOpacity(0.3),
              BlendMode.darken,
            ),
          ),
        ),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            // 城市名称和日期
            Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: [
                Text(
                  city,
                  style: const TextStyle(
                    fontSize: 24,
                    fontWeight: FontWeight.bold,
                    color: Colors.white,
                  ),
                ),
                Text(
                  DateFormat('MM/dd').format(DateTime.now()),
                  style: const TextStyle(fontSize: 16, color: Colors.white),
                ),
              ],
            ),

            const SizedBox(height: 20),

            // 主要天气信息
            Row(
              children: [
                // 天气图标
                Container(
                  width: 80,
                  height: 80,
                  decoration: BoxDecoration(
                    color: Colors.blue.withOpacity(0.1),
                    shape: BoxShape.circle,
                  ),
                  child: Icon(
                    _getWeatherIcon(weather.skycon),
                    size: 50,
                    color: Colors.blue,
                  ),
                ),

                const SizedBox(width: 20),

                // 温度和天气描述
                Expanded(
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      Text(
                        '${weather.temperature.round()}°',
                        style: const TextStyle(
                          fontSize: 48,
                          fontWeight: FontWeight.bold,
                          color: Colors.white,
                        ),
                      ),
                      Text(
                        _getWeatherDescription(weather.skycon),
                        style: const TextStyle(
                          fontSize: 16,
                          color: Colors.white,
                        ),
                      ),
                    ],
                  ),
                ),
              ],
            ),

            const SizedBox(height: 20),

            // 详细信息
            Row(
              mainAxisAlignment: MainAxisAlignment.spaceAround,
              children: [
                _buildDetailItem(
                  Icons.water_drop,
                  '湿度',
                  '${weather.humidity}%',
                ),
                _buildDetailItem(Icons.air, '风力', '${weather.windSpeed} km/h'),
                _buildDetailItem(Icons.eco, '空气质量', weather.aqiDescription),
                _buildDetailItem(Icons.visibility, 'AQI', '${weather.aqi}'),
              ],
            ),
          ],
        ),
      ),
    );
  }

  Widget _buildDetailItem(IconData icon, String label, String value) {
    return Column(
      children: [
        Icon(icon, color: Colors.blue, size: 20),
        const SizedBox(height: 4),
        Text(label, style: const TextStyle(fontSize: 12, color: Colors.white)),
        const SizedBox(height: 2),
        Text(
          value,
          style: const TextStyle(
            fontSize: 14,
            fontWeight: FontWeight.w500,
            color: Colors.white,
          ),
        ),
      ],
    );
  }

  IconData _getWeatherIcon(String skycon) {
    switch (skycon) {
      case 'CLEAR_DAY':
        return Icons.wb_sunny;
      case 'CLEAR_NIGHT':
        return Icons.nightlight_round;
      case 'PARTLY_CLOUDY_DAY':
      case 'PARTLY_CLOUDY_NIGHT':
        return Icons.cloud;
      case 'CLOUDY':
        return Icons.cloud;
      case 'LIGHT_HAZE':
      case 'MODERATE_HAZE':
      case 'HEAVY_HAZE':
        return Icons.waves;
      case 'LIGHT_RAIN':
      case 'MODERATE_RAIN':
      case 'HEAVY_RAIN':
      case 'STORM_RAIN':
        return Icons.grain;
      case 'LIGHT_SNOW':
      case 'MODERATE_SNOW':
      case 'HEAVY_SNOW':
      case 'STORM_SNOW':
        return Icons.ac_unit;
      case 'DUST':
      case 'SAND':
        return Icons.waves;
      case 'FOG':
        return Icons.waves;
      case 'WIND':
        return Icons.air;
      default:
        return Icons.wb_sunny;
    }
  }

  String _getWeatherDescription(String skycon) {
    switch (skycon) {
      case 'CLEAR_DAY':
        return '晴天';
      case 'CLEAR_NIGHT':
        return '晴夜';
      case 'PARTLY_CLOUDY_DAY':
        return '多云';
      case 'PARTLY_CLOUDY_NIGHT':
        return '多云';
      case 'CLOUDY':
        return '阴天';
      case 'LIGHT_HAZE':
        return '轻度雾霾';
      case 'MODERATE_HAZE':
        return '中度雾霾';
      case 'HEAVY_HAZE':
        return '重度雾霾';
      case 'LIGHT_RAIN':
        return '小雨';
      case 'MODERATE_RAIN':
        return '中雨';
      case 'HEAVY_RAIN':
        return '大雨';
      case 'STORM_RAIN':
        return '暴雨';
      case 'LIGHT_SNOW':
        return '小雪';
      case 'MODERATE_SNOW':
        return '中雪';
      case 'HEAVY_SNOW':
        return '大雪';
      case 'STORM_SNOW':
        return '暴雪';
      case 'DUST':
        return '浮尘';
      case 'SAND':
        return '沙尘';
      case 'FOG':
        return '雾';
      case 'WIND':
        return '大风';
      default:
        return '未知';
    }
  }

  String _getWeatherBackground(String skycon) {
    switch (skycon) {
      case 'CLEAR_DAY':
        return 'assets/weather_bg/clear_day.jpg';
      case 'CLEAR_NIGHT':
        return 'assets/weather_bg/clear_night.jpg';
      case 'PARTLY_CLOUDY_DAY':
      case 'PARTLY_CLOUDY_NIGHT':
        return 'assets/weather_bg/partly_cloudy.jpg';
      case 'CLOUDY':
        return 'assets/weather_bg/cloudy.jpg';
      case 'LIGHT_HAZE':
      case 'MODERATE_HAZE':
      case 'HEAVY_HAZE':
        return 'assets/weather_bg/haze.jpg';
      case 'LIGHT_RAIN':
      case 'MODERATE_RAIN':
      case 'HEAVY_RAIN':
      case 'STORM_RAIN':
        return 'assets/weather_bg/rain.jpg';
      case 'LIGHT_SNOW':
      case 'MODERATE_SNOW':
      case 'HEAVY_SNOW':
      case 'STORM_SNOW':
        return 'assets/weather_bg/snow.jpg';
      case 'DUST':
      case 'SAND':
        return 'assets/weather_bg/dust.jpg';
      case 'FOG':
        return 'assets/weather_bg/fog.jpg';
      case 'WIND':
        return 'assets/weather_bg/wind.jpg';
      default:
        return 'assets/weather_bg/default.jpg';
    }
  }
}
